package com.example.caishijitable.view;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;

import com.example.caishijitable.R;

public class MainActivity extends AppCompatActivity {

    WebView mWebView;

    private String webviewurl;
    private TextView tvTitle;

    private String BaseUrl;

    SharedPreferences sp;
    SharedPreferences.Editor edit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //去掉标题
        makeStatusBarTransparent(this);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mWebView = (WebView)findViewById(R.id.compre_webview);
        tvTitle = findViewById(R.id.tv_main_title);





        sp = getSharedPreferences("position", Context.MODE_PRIVATE);
        edit = sp.edit();

//        BaseUrl = sp.getString("ip", "");
        BaseUrl = "http://220.178.232.19:8083/data";

//        tvTitle.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                final EditText et = new EditText(MainActivity.this);
//                et.setHint("http://192.168.20.59:8080");
//
////                若已经保存ip，则显示
//                if (!sp.getString("ip", "").equals("")) {
//                    et.setText(sp.getString("ip", ""));
//                }
//                new AlertDialog.Builder(MainActivity.this)
//                        .setMessage("请输入ip地址")
//                        .setView(et)
//                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                            public void onClick(DialogInterface dialog, int id) {
//                                edit.putString("ip", et.getText().toString().trim());
//                                edit.commit();
//                                BaseUrl = sp.getString("ip", "");
//                                initWebView();
////                                String onlineUrl =  BaseUrl + "/#/pages/commonReport/commonReport";
////                                mWebView.loadUrl(onlineUrl);
//
//                            }
//                        })
//                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                            public void onClick(DialogInterface dialog, int id) {
//
//                            }
//                        }).show();
//            }
//        });

        initWebView();
    }

    private void initWebView() {
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setAllowFileAccess(true); //设置可以访问文件
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        mWebView.getSettings().setLoadsImagesAutomatically(true); //支持自动加载图片
        mWebView.getSettings().setBlockNetworkImage(false);//解决图片不显示
        mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");//设置编码格式

        // 这是h5 交互
//        mWebView.addJavascriptInterface(new schedule(), "booking");
//        mWebView.addJavascriptInterface(new noLogin(), "noLogin");

        mWebView.getSettings().setAppCacheEnabled(true);
//        mWebView.setWebViewClient(new WebViewClient() {
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                super.onPageFinished(view, url);
//                String token;
//
//
////                String method = "javascript:getToken(\"" + token + "\")";
////                mWebView.loadUrl(method);
//
//            }
//
//            @Override
//            public void onLoadResource(WebView view, String url) {
//                webviewurl = view.getUrl();
//                super.onLoadResource(view, url);
//            }
//
//
//            @Override
//            public void onPageStarted(WebView view, String url, Bitmap favicon) {
//                try {
//                    //iosLoadDialog.show();
//                } catch (Exception e) {
//                    Log.e("erro", Log.getStackTraceString(e));
//                }
//                super.onPageStarted(view, url, favicon);
//            }
//
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                if (url == null)
//                    return false;
//                if (url.startsWith("http:") || url.startsWith("https:")) {
//                    view.loadUrl(url);
//                    return false;
//                } else {
//                    try {
//                        Intent intent = new Intent(Intent.ACTION_VIEW);
//                        intent.setData(Uri.parse(url));
//                        startActivity(intent);
//                    } catch (Exception e) {
////                    ToastUtils.showShort("暂无应用打开此链接");
//                    }
//                    return true;
//                }
//
//            }
//
//            @Override
//            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                handler.proceed();// 接受所有网站的证书
//            }
//        });
//        mWebView.setWebChromeClient(new WebChromeClient() {
//            @Override
//            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//                return super.onJsAlert(view, url, message, result);
//            }
//
//        });

        // 这里 做页面跳转
//        String onlineUrl = (String) SPUtil.get(getContext(), BaseConfig.ANALYSE_PATH, "");
//        String onlineUrl = "http://192.168.20.59:8080/#/pages/commonReport/commonReport";
        String onlineUrl =  BaseUrl + "/#/pages/commonReport/commonReport";
//        String onlineUrl = "http://www.baidu.com";
//        String onlineUrl = BaseConfig.WEB_Base_URL + "#/pages/comprehensiveDisplay/comprehensiveDisplay";
//        String onlineUrl = "http://192.168.20.58:8081/#/pages/comprehensiveDisplay/comprehensiveDisplay";
        mWebView.loadUrl(onlineUrl);
    }

    public static void makeStatusBarTransparent(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            int option = window.getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            window.getDecorView().setSystemUiVisibility(option);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }
}
